<template>
   <div class="app-container">
      <div class="p4">
         <FaSearchBar :show-toggle="false">
            <template #default="{ fold, toggle }">
               <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="80px">
                  <el-form-item label="设备" prop="device">
                     <el-input v-model="queryParams.device" placeholder="请输入设备" clearable style="width: 150px;"
                        @keyup.enter="handleQuery" />
                  </el-form-item>
                  <el-form-item label="用户名" prop="username">
                     <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable style="width: 150px;"
                        @keyup.enter="handleQuery" />
                  </el-form-item>
                  <el-form-item label="登录地点" prop="location" v-show="!fold">
                     <el-input v-model="queryParams.location" placeholder="请输入登录地点" clearable style="width: 150px;"
                        @keyup.enter="handleQuery" />
                  </el-form-item>
                  <el-form-item label="状态" prop="status" v-show="!fold">
                     <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 100px">
                        <el-option v-for="dict in sys_oper_status" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
                     </el-select>
                  </el-form-item>
                  <el-form-item label="登录时间" style="width: 308px" v-show="!fold">
                     <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"></el-date-picker>
                  </el-form-item>
                  <el-form-item>
                     <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                     <el-button @click="resetQuery">重置</el-button>
                     <el-button link @click="toggle">
                        <template #icon>
                           <FaIcon :name="fold ? 'i-ep:caret-bottom' : 'i-ep:caret-top'" />
                        </template>
                        {{ fold ? '高级查询' : '高级查询' }}
                     </el-button>
                  </el-form-item>
               </el-form>
            </template>
         </FaSearchBar>
         <el-divider border-style="dashed" style="margin:0px 0px 20px 0px" />

         <el-space wrap>

            <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete"
               v-auth="['login:log$remove']">删除</el-button>
            <el-button type="danger" icon="Delete" @click="handleClean" v-auth="['login:log$remove']">清空</el-button>
            <el-button type="warning" icon="Download" @click="handleExport" v.auth="['login:log$export']">导出</el-button>
         </el-space>

         <el-table ref="loginlogRef" v-loading="loading" :data="loginlogList" @selection-change="handleSelectionChange"
            :default-sort="defaultSort" @sort-change="handleSortChange" stripe highlight-current-row border class="mt4">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="#" align="center" prop="id" width="180" />
            <el-table-column label="用户名" align="center" prop="username" :show-overflow-tooltip="true" sortable="custom"
               :sort-orders="['descending', 'ascending']" />
            <el-table-column label="授权模式" align="center" prop="grantType" :show-overflow-tooltip="true" />
            <el-table-column label="登录IP" align="center" prop="ip" :show-overflow-tooltip="true" />
            <el-table-column label="登录地点" align="center" prop="location" :show-overflow-tooltip="true" />
            <el-table-column label="设备" align="center" prop="device" :show-overflow-tooltip="true" />
            <el-table-column label="操作系统" align="center" prop="os" :show-overflow-tooltip="true" />
            <el-table-column label="浏览器" align="center" prop="browser" :show-overflow-tooltip="true" />
            <el-table-column label="状态" align="center" prop="status">
               <template #default="scope">
                  <DictTag :dictData="sys_oper_status" :value="scope.row.status + ''" />
               </template>
            </el-table-column>
            <el-table-column label="描述" align="center" prop="msg" :show-overflow-tooltip="true" />
            <el-table-column label="登录时间" align="center" prop="loginAt" sortable="custom"
               :sort-orders="['descending', 'ascending']" width="180">
               <template #default="scope">
                  <span>{{ parseTime(scope.row.loginAt) }}</span>
               </template>
            </el-table-column>
         </el-table>

         <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />
      </div>
   </div>
</template>

<script setup name="loginlog">
import apiLoginLog from "@/api/monitor/loginlog";

const { proxy } = getCurrentInstance();
const { sys_oper_status } = proxy.useDict("sys_oper_status");

const loginlogList = ref([]);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const selectName = ref("");
const total = ref(0);
const dateRange = ref([]);
const defaultSort = ref({ prop: "loginAt", order: "descending" });

// 查询参数
const queryParams = ref({
   pageNum: 1,
   pageSize: 10,
   location: undefined,
   username: undefined,
   device: undefined,
   status: undefined,
   orderByColumn: undefined,
   isAsc: undefined
});

/** 查询登录日志列表 */
function getList() {
   // loading.value = true;
   apiLoginLog.page(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
      loginlogList.value = response.data.rows;
      total.value = response.data.total;
   }).finally(() => {
      loading.value = false
   })
}
/** 搜索按钮操作 */
function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
}
/** 重置按钮操作 */
function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   queryParams.value.pageNum = 1;
   proxy.$refs["loginlogRef"].sort(defaultSort.value.prop, defaultSort.value.order);
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.id);
   multiple.value = !selection.length;
   single.value = selection.length != 1;
   selectName.value = selection.map(item => item.username);
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
   queryParams.value.orderByColumn = column.prop;
   queryParams.value.isAsc = column.order;
   getList();
}
/** 删除按钮操作 */
function handleDelete(row) {
   const llids = row.id || ids.value;
   proxy.$modal.confirm('是否确认删除访问编号为"' + llids + '"的数据项?').then(function () {
      return apiLoginLog.delete(llids);
   }).then(() => {
      getList();
      proxy.$modal.success("删除成功");
   }).catch(() => { });
}
/** 清空按钮操作 */
function handleClean() {
   proxy.$modal.confirm("是否确认清空所有登录日志数据项?").then(function () {
      return apiLoginLog.clean();
   }).then(() => {
      getList();
      proxy.$modal.success("清空成功");
   }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
   apiLoginLog.download(queryParams.value, "登录日志");
}

getList();
</script>
